package com.jp.study.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import com.jp.study.Interceptor.MethodConsumedTimeRecorder;
import com.jp.study.annotation.LogConsumedTime;
import com.jp.study.properties.JPConfigProperties;
import com.jp.study.properties.swagger.JPApiInfo;
import com.jp.study.properties.swagger.JPSwaggerProperties;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 
 * @author dinghao
 * @ClassName: JPConfig
 * @Description: 配置类
 * @date 2018年3月21日 上午9:57:20
 *
 */

@EnableAspectJAutoProxy
@Configuration
@EnableConfigurationProperties(JPConfigProperties.class)
@EnableSwagger2
public class JPConfig {

	// 日志
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private JPSwaggerProperties swaggerProperties;

	@Bean
	public Docket docket() {

		Docket docket;
		if (null == swaggerProperties.getApiInfo() || null == swaggerProperties.getBasePackage()) {
			docket = new Docket(DocumentationType.SWAGGER_2);
		} else {
			docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo(swaggerProperties.getApiInfo())).select()
					.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage())).build();
			logger.info("成功实例化swagger：Docket");
		}
		return docket;
	}

	private ApiInfo apiInfo(JPApiInfo jpApiInfo) {
		ApiInfo apiInfo = new ApiInfoBuilder().title(jpApiInfo.getTitle()).description(jpApiInfo.getDescription())
				.contact(new Contact(jpApiInfo.getContact(), jpApiInfo.getUrl(), jpApiInfo.getEmail()))
				.version(jpApiInfo.getVersion()).build();
		return apiInfo;
	}

	/**
	 * 使用日志记录方法执行前后的耗时
	 * 
	 * @return
	 */
	@Bean("methodConsumedTimeRecorder")
	public PointcutAdvisor registerMethodConsumedTimeRecorder() {
		Pointcut pointcut = new AnnotationMatchingPointcut(null, LogConsumedTime.class);
		MethodInterceptor interceptor = new MethodConsumedTimeRecorder();
		DefaultBeanFactoryPointcutAdvisor pointcutAdvisor = new DefaultBeanFactoryPointcutAdvisor();
		pointcutAdvisor.setAdvice(interceptor);
		pointcutAdvisor.setPointcut(pointcut);
		pointcutAdvisor.setOrder(0);
		logger.info("成功实例化MethodConsumedTimeRecorder的切面");
		return pointcutAdvisor;
	}
	

}